package 算法.NiuKe.JZ;

import 算法.NiuKe.TreeNode;

import java.util.Stack;

/**
 * @author:谢君臣
 * @Date:2021/4/1221:02
 * @version:1.0
 * @function: 输入一棵二叉树，求该树的深度。从根结点到叶结点依次经过的结点（含根、叶结点）形成树的一条路径，
 *              最长路径的长度为树的深度。
 */
public class JZ38 {
    public static void main(String[] args) {

    }
    public int TreeDepth(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        if (root==null) {
            return 0;
        }
        int deep = 0,max = 0;
        while (!stack.isEmpty()||root!=null){
            while (root!=null) {
                stack.push(root);
                root = root.left;
                deep++;
            }
            max = Math.max(deep, max);
            if (!stack.isEmpty()) {
                root = stack.pop();
                deep--;
                if (root.right!=null){
                    root = root.right;
                    deep++;
                }else root = null;
            }
        }
        return max;
    }
}
